package com.dunshan.prcexp.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.Predicate;
import lombok.extern.log4j.Log4j2;

import java.util.Iterator;
import java.util.Map;

/**
 * @author liwen406
 * @program: dunshan-pef
 * @description:
 * @date 2022-08-25 10:25:45
 */
@Log4j2
public class JsonUtil {
    public JsonUtil() {
    }

    public static String order(String json) {
        String orderString = "";

        try {
            orderString = JSON.toJSONString(JSON.parseObject(json), new SerializerFeature[]{SerializerFeature.MapSortField, SerializerFeature.WriteMapNullValue});
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return orderString;
    }

    public static String prettyPrint(String source) {
        String orderString = "";

        try {
            orderString = JSON.toJSONString(JSON.parseObject(source), new SerializerFeature[]{SerializerFeature.MapSortField, SerializerFeature.WriteMapNullValue, SerializerFeature.PrettyFormat});
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return orderString;
    }

    public static Object jsonPath(String json, String path, Filter... filter) {
        try {
            return filter.length == 0 ? JsonPath.parse(json).read(path, new Predicate[0]) : JsonPath.parse(json).read(path, new Predicate[]{filter[0]});
        } catch (PathNotFoundException var4) {
            log.error(var4.getMessage());
            return null;
        }
    }

    public static Object jsonPathWeb(String json, String path, Filter... filter) {
        try {
            return filter.length == 0 ? JsonPath.parse(json).read(path, new Predicate[0]) : JsonPath.parse(json).read(path, new Predicate[]{filter[0]});
        } catch (PathNotFoundException var4) {
            return null;
        }
    }

    public static String remove(String json, String path, Filter... filter) {
        try {
            return filter.length == 0 ? JsonPath.parse(json).delete(path, new Predicate[0]).jsonString() : JsonPath.parse(json).delete(path, new Predicate[]{filter[0]}).jsonString();
        } catch (PathNotFoundException var4) {
            log.error(var4.getMessage());
            return null;
        }
    }

    public static String remove(String json, Map<String, Filter> pathMap) {
        String localJson = json;
        Iterator var3 = pathMap.entrySet().iterator();

        while(var3.hasNext()) {
            Map.Entry<String, Filter> entry = (Map.Entry)var3.next();
            if (entry.getValue() != null) {
                localJson = JsonPath.parse(localJson).delete((String)entry.getKey(), new Predicate[0]).jsonString();
            } else {
                localJson = JsonPath.parse(localJson).delete((String)entry.getKey(), new Predicate[]{(Predicate)entry.getValue()}).jsonString();
            }
        }

        return localJson;
    }
}
